package zqkcom.atguigu.dao.impl;

import zqkcom.atguigu.bean.Page;
import zqkcom.atguigu.dao.BaseDao;
import zqkcom.atguigu.dao.BookDao;
import zqkcom.atguigu.bean.Book;

import java.util.List;

public class BookDaoImpl extends BaseDao<Book> implements BookDao {
    @Override
    public List<Book> getBookPageByPrice(Page<Book> page, Integer minPrice, Integer maxPrice) {
        String sql = "select * from bs_book where price between ? and ? limit ?,?";
        return getBeanList(Book.class, sql, minPrice, maxPrice, page.getIndex(), page.getPageSize());

    }

    @Override
    public Integer getCountByPrice(Integer minPrice, Integer maxPrice) {
        String sql = "select count(*) from bs_book where price between ? and ?";
        Long singleData = (Long) getSingleData(sql, minPrice, maxPrice);
        return singleData.intValue();

    }

    @Override
    public List<Book> getBookPage(Page<Book> page) {
        String sql = "select * from bs_book limit ?,?";
        return getBeanList(Book.class, sql, page.getIndex(), page.getPageSize());
    }

    @Override
    public Integer getCount() {
        String sql = "select count(*) from bs_book";
        Long singleDate = (Long)  getSingleData(sql);
        return singleDate.intValue();
    }


//    @Override 删除了
//    public List<Book> getBookPage(Integer pageNo, Integer pageSize) {
//        String sql = "select * from bs_book limit ?,?";
//        return getBeanList(Book.class, sql,(pageNo-1)*pageSize,pageSize);//当前页数的索引
//
//    }


    @Override
    public void addBook(Book book) {
        String sql = "insert into bs_book values(null,?,?,?,?,?,?)";

//        update(sql, book.getTitle(), book.getAuthor(), book.getSales(), book.getStock(), book.getImgPath());
        update(sql, book.getTitle(), book.getAuthor(), book.getPrice(), book.getSales(), book.getStock(), book.getImgPath());

    }

    @Override
    public void deleteBook(Integer id) {
        String sql = "delete from bs_book where id=?";
        update(sql, id);
    }

    @Override
    public Book getBookByBookId(Integer id) {
        String sql = "select * from bs_book where id = ?";

        return getBean(Book.class, sql,id);
    }

    @Override
    public void updateBook(Book book) {
        String sql = "update bs_book set title=? ,author=? ,price =? ,sales = ?,stock= ?,imgPath =? where id=?";
        update(sql, book.getTitle(), book.getAuthor(), book.getPrice(),book.getSales(), book.getStock(), book.getImgPath(), book.getId());



    }
//    更新图书的库存和销量
    @Override
    public void updateSalesAndStock(Integer bookId, Integer count) {
        String sql = "update bs_book set sales = sales + ? and stock = stock - ? where id = ?";
        update(sql,count,count,bookId);//因为有两次。
    }
}
